layui.use([ 'form','layer','jquery','table','laydate'], function() {
    var layer = layui.layer, $ = layui.jquery, form = layui.form,table=layui.table,laydate = layui.laydate;


    active = {
        search : function(){
            var number = $('#number');

            table.reload('CarInfoList',{
                page : {
                    curr : 1
                },
                where : {
                    number : number.val(),

                }
            });
        }
    };

    table.render({
         id:'CarInfoList'
        ,elem: '#CarInfoList'
        ,url: ctx+'/CarInfo/getAllCarInfoList'// 数据接口
        ,limit:10// 每页默认数
        ,limits:[10,20,30,40]
        ,cols: [[ // 表头
            {field:'carId', title: '车辆ID', align: 'center',width:160}
            ,{field:'number', title: '车牌号' , align: 'center',templet : '#sexTpl',width:160}
            ,{field:'teamId', title: '所属车队' , align: 'center',width:170}
            ,{field:'type', title: '车辆类型', align: 'center' ,width:130}
            ,{field:'seatnum', title: '座号', align: 'center' ,width:130}
            ,{field:'busMasterId', title: '车辆负责人' , align: 'center',width:200}
            ,{field:'status', title: '状态', width:80, templet : '#statusTpl',width:95, align: 'center'}
            ,{field:'note', title: '备注', align: 'center' }
            , {field: 'right', title: '操作', align: 'center', toolbar: "#barDemo",width:150}
        ]]
            ,page: true // 开启分页
            ,loading:true
            ,where: {timestamp: (new Date()).valueOf()}
    });

    table.on('tool(CarInfoList)', function (obj) {
        var data = obj.data;
        if (obj.event === 'delete') {
            layer.confirm('确定要删除 '+data.number+' 么？', function (index) {
                $.ajax({
                    url : ctx + '/CarInfo/deleteCarInfoById',
                    type : "POST",
                    data: {"carId": data.carId},
                    success : function(d) {
                        if (d.code == 0) {
                            layer.msg("删除成功！",{icon: 1});
                            obj.del();
                        } else {
                            layer.msg("权限不足，删除失败！", {
                                icon : 5
                            });
                        }
                    },
                    error:function(){
                        layer.msg("删除失败！网络错误！",{icon: 5});
                    }
                })
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            var editIndex = layer.open({
                type : 2,
                title : "编辑车辆信息",
                area : [ '450px', '600px' ],
                content : ctx + "/CarInfo/editCarInfo/" + data.carId,
                success : function(layero, index) {

                }
            });
        }
    });

    $(".searchcar_btn").click(function() {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    })

    $(".CarInfoAdd_btn").click(function() {
        var addIndex = layer.open({
            title : "添加车辆",
            type : 2,
            area : [ '800px', '550px' ],
            content : ctx + "/CarInfo/addCarInfo",
            success : function(layero, index) {

            }
        })
    })

});